package cn.jarlen.jwt.tools.device;

import static android.content.Context.TELEPHONY_SERVICE;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.core.app.ActivityCompat;

import com.tdtech.devicemanager.DevicePolicyManager;
import com.tdtech.devicemanager.PeripheralPolicy;

import java.lang.reflect.Method;
import java.util.List;

import ga.mdm.PolicyManager;

public class DeviceUtils {

    public static final String TAG = DeviceUtils.class.getSimpleName();

    public static String getImeiByDevicePolicyManager() {
        String deviceId = "";
        try {
            String[] deviceInfos = PolicyManager.getInstance().getDeviceInfo();
            deviceId = deviceInfos[0];
        } catch (Exception e) {
            Log.w(TAG, "尝试用部标接口获取IMEI失败", e);
        } catch (Throwable throwable) {
            Log.w(TAG, "尝试用部标接口获取IMEI失败", throwable);
        }
        return deviceId;
    }

    public static String getImeiByDingQiaoPolicyManager(Context context) {
        DevicePolicyManager mDevicePolicyManager = DevicePolicyManager.getInstance(context);
        PeripheralPolicy mPeripheralPolicy = mDevicePolicyManager.getPeripheralPolicy();
        if (mPeripheralPolicy == null) {
            return "";
        }
        List<String> deviceInfo = null;
        try {
            deviceInfo = mPeripheralPolicy.getDeviceInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (deviceInfo != null && deviceInfo.size() > 0) {
            return deviceInfo.get(0);
        } else {
            return "";
        }
    }

    @SuppressLint("MissingPermission")
    public static String getIMEIByAndroidApi(Context context) {
        String imei = "";
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                imei = telephonyManager.getImei();
            } else {
                Class clazz = telephonyManager.getClass();
                Method getImei = clazz.getDeclaredMethod("getImei");
                imei = (String) getImei.invoke(telephonyManager);
            }
        } catch (Exception e) {
            e.printStackTrace();
            imei = "";
        }
        return imei;
    }

    /**
     * 通过原生API获取IMSI信息
     *
     * @param context 应用上下文
     * @return
     */
    public static String getIMSIByAndroidApi(Context context) {
        String imsi = "";
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            imsi = telephonyManager.getSubscriberId();
        } catch (Exception e) {
            Log.w(TAG, "通过原生API获取IMSI信息失败", e);
        } catch (Throwable throwable) {
            Log.w(TAG, "尝试用原生API获取IMSI失败", throwable);
        }
        Log.d(TAG, "通过原生API获取IMSI信息:" + imsi);
        return imsi;
    }

    /**
     * 通过部标API获取IMSI信息
     *
     * @param context 应用上下文
     * @return
     */
    public static String getIMSIByPolicyManager(Context context) {
        String imsi = "";
        try {
            String[] deviceInfos = PolicyManager.getInstance().getDeviceInfo();
            imsi = deviceInfos[13];
        } catch (Exception e) {
            Log.w(TAG, "尝试用部标接口获取IMSI失败", e);
        } catch (Throwable throwable) {
            Log.w(TAG, "尝试用部标接口获取IMSI失败", throwable);
        }
        Log.d(TAG, "通过部标API获取IMSI信息:" + imsi);
        return imsi;
    }

    public static String getMobilePhoneByAndroidApi(Context context) {
        String number = "";
        if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_NUMBERS)
                && PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)) {
            Log.w(TAG, "通过原生接口获取手机号，未获取到权限:READ_PHONE_NUMBERS|READ_PHONE_STATE");
            return number;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
            /*通过数据流量卡来判断正在使用的哪一张卡，从而返回手机号*/
            int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                subId = SubscriptionManager.getDefaultDataSubscriptionId();
            } else {
                try {
                    Class cls = SubscriptionManager.class.getClass();
                    Method method = cls.getDeclaredMethod("getDefaultDataSubId");
                    subId = (Integer) method.invoke(subscriptionManager);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            SubscriptionInfo subscriptionInfo = subscriptionManager.getActiveSubscriptionInfo(subId);
            number = subscriptionInfo.getNumber();
        } else {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
            number = telephonyManager.getLine1Number();
        }
        return number;
    }

    public static void main(String[] args) {
        boolean fa = 1677469468364L + 1673690030758L < 1677469675958L + 60000;
        System.out.println("====fa:" + fa);
    }
}
